package com.zjlanyun.eplus.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zjlanyun.eplus.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DatePickerDialogUtil implements OnDateChangedListener {

	private DatePicker datePicker;
	private AlertDialog ad;
	private String dateTime;
	private String initDateTime;
	private Activity activity;
	private boolean showTitle = false;

	/**
	 * 日期时间弹出选择框构造函数
	 * 
	 * @param activity
	 *            ：调用的父activity
	 * @param initDateTime
	 *            初始日期时间值，作为弹出窗口的标题和日期时间初始值
	 */
	public DatePickerDialogUtil(Activity activity, String initDateTime,
			boolean showTitle) {
		this.activity = activity;
		this.initDateTime = initDateTime;
		this.showTitle = showTitle;

	}

	public void init(DatePicker datePicker) {
		Calendar calendar = Calendar.getInstance();
		if (!(null == initDateTime || "".equals(initDateTime))) {
			calendar = this.getCalendarByInintData(initDateTime);
		} else {
			initDateTime = calendar.get(Calendar.YEAR) + "-"
					+ calendar.get(Calendar.MONTH) + "-"
					+ calendar.get(Calendar.DAY_OF_MONTH) + "-";
		}

		datePicker.init(calendar.get(Calendar.YEAR),
				calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH), this);
	}

	/**
	 * 弹出日期时间选择框方法
	 * 
	 * @param inputDate
	 *            :为需要设置的日期时间文本编辑框
	 * @return
	 */
	public AlertDialog datePickerAlertDialog(final TextView inputDate) {
		LinearLayout dateTimeLayout = (LinearLayout) activity
				.getLayoutInflater().inflate(R.layout.datepicker, null);
		datePicker = (DatePicker) dateTimeLayout.findViewById(R.id.datepicker);
		init(datePicker);

		if (showTitle) {
			ad = new AlertDialog.Builder(activity)
					.setTitle(initDateTime)
					.setView(dateTimeLayout)
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									inputDate.setText(dateTime);
								}
							})
					.setNegativeButton("取消",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									// inputDate.setText("");
								}
							}).show();
		} else {
			ad = new AlertDialog.Builder(activity)
					.setView(dateTimeLayout)
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									inputDate.setText(dateTime);
								}
							})
					.setNegativeButton("取消",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									// inputDate.setText("");
								}
							}).show();
		}

		onDateChanged(null, 0, 0, 0);
		return ad;
	}

	@SuppressLint("SimpleDateFormat")
	public void onDateChanged(DatePicker view, int year, int monthOfYear,
			int dayOfMonth) {
		// 获得日历实例
		Calendar calendar = Calendar.getInstance();

		calendar.set(datePicker.getYear(), datePicker.getMonth(),
				datePicker.getDayOfMonth());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		dateTime = sdf.format(calendar.getTime());
		if (showTitle) {
			ad.setTitle(dateTime);
		}
	}

	/**
	 * 实现将初始日期时间2012年07月02日拆分成年 月 日 时 分 秒,并赋值给calendar
	 * 
	 * @param initDateTime
	 *            初始日期时间值 字符串型
	 * @return Calendar
	 */
	private Calendar getCalendarByInintData(String initDateTime) {
		Calendar calendar = Calendar.getInstance();

		String[] dateStr = initDateTime.split("-");

		int currentYear = Integer.valueOf(dateStr[0].trim()).intValue();
		int currentMonth = Integer.valueOf(dateStr[1].trim()).intValue() - 1;
		int currentDay = Integer.valueOf(dateStr[2].trim()).intValue();

		calendar.set(currentYear, currentMonth, currentDay);
		return calendar;
	}
}
